package com.itheima.springbootcondition.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;
import redis.clients.jedis.Jedis;

import java.util.Map;

public class ClassCondition implements Condition {

    //根据matches的判断来决定是否可以获取到数据
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //context指的是上下文对象，metadata指的是元数据

        //需求1.是需要导入Jedis坐标之后才可以获取到对象
//        Boolean flag = true;
//        try {
//            Class<?> aClass = Class.forName("redis.clients.jedis.Jedis");
//        } catch (ClassNotFoundException e) {
//            flag = false;
//        }
//
//        return flag;


        //需求2.需要动态的获取注解定义的数据
        //获取到mapd对象
        Map<String, Object> map = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
//        System.out.println(map);

        //获取到对应的String[]
        String[] value = (String[]) map.get("value");

        Boolean flag = true;
        try {
            for (String ClassName : value) {
                //遍历数组，进行查询判断jedis坐标是否导入
                Class<?> aClass = Class.forName(ClassName);
            }
        } catch (ClassNotFoundException e) {
            flag = false;
        }

        return flag;

    }
}
